﻿
Imports elpmis
Imports lpmsengi
Imports idsysctx
Imports occommon

Public Class userGetUI
    Inherits requestProcessor

    Public Const _pathidentity As String = network.pathSeperator + path.userGetUI

    Shared Sub New()
        assert(Not strstartwith(path.userGetUI, network.pathSeperator))
    End Sub

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Shared Function processLogin(ByVal context As httpContext, _
                                         ByVal idctx As idcontext, _
                                         Optional ByVal id As String = Nothing) As Boolean
        elpmis.context.insertThreadStaticVariable("loginUrl", typeDefination.string, idctx.loginUrl())
        elpmis.context.insertThreadStaticVariable("id", typeDefination.string, id)
        Return context.createControl(New nameLpmsControl("login")).render()
    End Function

    Private Shared Function processSetUI(ByVal context As httpContext, _
                                         ByVal idctx As idcontext, _
                                         ByVal id As String) As Boolean
        elpmis.context.insertThreadStaticVariable("id", typeDefination.string, id)
        Dim sharedCookies As String = Nothing
        If Not userSharedCookies(id, sharedCookies) Then
            raiseError("cannot get shared cookies for id ", id, ", assume empty.")
        End If
        strrplc(sharedCookies, cookieSeparator, contentSeparator)
        elpmis.context.insertThreadStaticVariable("sharedCookies", typeDefination.string, sharedCookies)
        elpmis.context.insertThreadStaticVariable("forceUpdateUrl", typeDefination.string, idctx.forceUpdateLoginUrl())
        Return context.createControl(New nameLpmsControl("setui")).render()
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        Dim id As String = Nothing
        Dim idctx As idcontext = Nothing
        If context.userInput(idcommon.query.id, cookie.user, id) Then
            Dim usrk As String = Nothing
            If context.userInput(idcommon.query.userSiteRandomKey, cookie.userSiteRandomKey, usrk) Then
                idctx = createIdContext(id, usrk, context, True)
                If idctx.loggedIn() Then
                    context.setforevercookie(cookie.user, id)
                    context.setforevercookie(cookie.userSiteRandomKey, usrk)
                    Return processSetUI(context, idctx, id)
                Else
                    Return processLogin(context, idctx, id)
                End If
            End If
        End If

        assert(idctx Is Nothing)
        idctx = createIdContext(Nothing, Nothing, context, False)
        Return processLogin(context, idctx)
    End Function
End Class
